﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace tamlytre.Admin
{
    public partial class PostNews : System.Web.UI.Page
    {
        DBTuVanDataContext db = new DBTuVanDataContext();
        public static int idchild = 0;
        public static int idparent = 0;
        public static TB_User taiKhoan = null;
        public static string filename;
        public static int count = 0;
        public void loadCategory()
        {
            
            bool bSuc1 = Int32.TryParse(Request.QueryString["id"], out idchild);
            if (idchild == 1)
                Response.Redirect("../Default.aspx");
            else
            {
                if (idchild != 0)
                {
                    List<TB_Catagory> child = db.USP_Category(idchild).ToList();
                    DropDownListChude.DataSource = child;
                    DropDownListChude.DataValueField = "CateID";
                    DropDownListChude.DataTextField = "CateName";
                    DropDownListChude.DataBind();
                }
            }
             
        }
        
        public int getCateID()
        {
            if (DropDownListChude.SelectedItem != null)
            {
                return Convert.ToInt32(DropDownListChude.SelectedValue.Trim());
            }
            return idchild;
        }
        public void loadNews()
        {
            int cateid = getCateID();
            try
            {
                List<TB_New> loadnews = db.USP_LoadNewsCate(cateid).ToList();
                ListViewUsers.DataSource = loadnews;
                ListViewUsers.DataBind();
                count = loadnews.Count();
            }
            catch
            {
                Response.Redirect("Default.aspx");
            }
        }
        public string CollectData()
        {
            taiKhoan = (TB_User)Session["TaiKhoan"];
           
            return taiKhoan.UserName.Trim();
            
        }
        public void uppicture()
        {
            HttpPostedFile myFile = FileUploadPicture.PostedFile;
            if (myFile.ContentLength / (1024 * 1024) > 2)
            {
                LabelMessage.Text = "Kích thước file lớn hơn 2Mb. Chọn một file khác !";
            }
            else
            {
                filename = myFile.FileName;
                bool checkfilename = db.TB_News.ToList().Exists(pt=> pt.Picture == filename);
                if (checkfilename == true)
                {
                    filename = "1_"+ filename;
                }
                myFile.SaveAs(Server.MapPath("../images/" + filename));
            }
        }
        public TB_New getNewsDangBai()
        {
            TB_New news = new TB_New();
            news.Title = txtTieude.Text;
            news.Cate_ID = getCateID();
            news.MainContent = FCKTitle.Value;
            news.Content = FCKContent.Value;
            news.CreateDate = DateTime.Now;
            news.PublishDate = DateTime.Now;
            news.Author = txtTacGia.Text;
            news.Keyword = txtkeyword.Text;
            news.PictureNote = txtGhichuhinh.Text;
            news.Link = txtLink.Text;
            news.Picture = filename;
            if (cbChonTieuDiem.Checked)
                news.NewsFocus = 1;
            else
                news.NewsFocus = 0;
            if (cbBaihay.Checked)
                news.BaiHay = true;
            else
                news.BaiHay = false;
            news.Status = 1;
            news.NumViews = 0;
            news.TitleNosign = txtTuKhoa.Text;
            news.User_Name = CollectData().Trim();
            news.UserPublish = CollectData().Trim();
            news.DaXoa = false;
            return news;
        }
        public TB_New getNewsLuubai()
        {
            TB_New news = new TB_New();
            news.Title = txtTieude.Text;
            news.Cate_ID = getCateID();
            news.MainContent = FCKTitle.Value;
            news.Content = FCKContent.Value;
            news.CreateDate = DateTime.Now;
            news.Author = txtTacGia.Text;
            news.Keyword = txtkeyword.Text;
            news.PictureNote = txtGhichuhinh.Text;
            news.Link = txtLink.Text;
            news.Picture = filename;
            if (cbChonTieuDiem.Checked)
                news.NewsFocus = 1;
            else
                news.NewsFocus = 0;
            if (cbBaihay.Checked)
                news.BaiHay = true;
            else
                news.BaiHay = false;
            news.Status = 0;
            news.NumViews = 0;
            news.TitleNosign = txtTuKhoa.Text;
            news.User_Name = CollectData();
            news.DaXoa = false;
            return news;
        }
        protected void ibtDangbai_Click(object sender, ImageClickEventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    uppicture();
                    TB_New tb = getNewsDangBai();
                    db.TB_News.InsertOnSubmit(tb);
                    db.SubmitChanges();
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript'>alert('Đăng bài thành công !');</script>");
                }
                catch
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript'>alert('Đăng bài thất bại !');</script>");
                }
            }
        }

        protected void ibtLuuKhongDang_Click(object sender, ImageClickEventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    uppicture();
                    TB_New tb = getNewsLuubai();
                    db.TB_News.InsertOnSubmit(tb);
                    db.SubmitChanges();
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript'>alert('Lưu bài thành công !');</script>");
                }
                catch 
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript'>alert('Lưu bài thất bại');</script>");
                }
            }
        }
        public bool DeleteNews(int id)
        {
            try
            {
                using (DBTuVanDataContext db = new DBTuVanDataContext())
                {
                    TB_New tk = db.TB_News.Single(t => t.idNews == id);
                    tk.DaXoa = true;
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public void DoDelete()
        {
            string action = Request.QueryString["del"];
            if (action == null)
                return;
            int idnews = 0;
            if (Int32.TryParse(action, out idnews))
            {
                DeleteNews(idnews);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadNews();
                loadCategory();
            }
            DoDelete();
        }
        protected void DataPager_PreRender(object sender, EventArgs e)
        {
            loadNews();
        }
    }
}